<template>
  <div v-if=" visible"
       class="aui-card--fill scroll-box">
    <div class="mod-sys__user pane-shell">
      <el-page-header @back="goBack"
                      class="margin-bottom"
                      content="详情页面">
      </el-page-header>
      <el-form :inline="true"
               @submit.native.prevent
               :model="dataForm"
               @keyup.enter.native="getPageInit()">
        <el-form-item>
          <el-select filterable
                     clearable
                     v-model.trim="dataForm.menuId"
                     :placeholder="$t('roleMenu.menuName')">
            <el-option v-for="item in menuList"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
                     @click="getPageInit()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item class="addBtn">
          <el-button v-if="$hasPermission('system:sysRoleMenu:save')"
                     type="primary"
                     plain
                     icon="el-icon-plus"
                     @click="addUser(dataForm)">{{ $t('add') }}角色菜单</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('system:sysRoleMenu:del')"
                     type="danger"
                     plain
                     @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading"
                :data="dataList"
                border
                @selection-change="dataListSelectionChangeHandle"
                @sort-change="dataListSortChangeHandle"
                style="width: 100%;">
        <el-table-column type="selection"
                         header-align="center"
                         align="center"
                         width="50"></el-table-column>
        <el-table-column prop="roleName"
                         :label="$t('roleMenu.roleName')"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column prop="menuName"
                         :label="$t('roleMenu.menuName')"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column :label="$t('handle')"
                         fixed="right"
                         header-align="center"
                         align="center"
                         width="150">
          <template slot-scope="scope">
            <el-button v-if="$hasPermission('system:sysRoleMenu:update') && $hasPermission('system:sysRoleMenu:getById')"
                       type="text"
                       size="small"
                       @click="addUser(scope.row)">{{ $t('update') }}</el-button>
            <el-button v-if="$hasPermission('system:sysRoleMenu:del')"
                       type="text"
                       size="small"
                       @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page="page"
                     :page-sizes="[10, 20, 50, 100]"
                     :total="total"
                     layout="total, sizes, prev, pager, next"
                     @size-change="pageSizeChangeHandle"
                     @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update ref="addOrUpdate"
                     :isAdd="isAdd"
                     @callBack="getDataList"></add-or-update>
    </div>
  </div>
</template>
<script>
import mixinViewModule from '@/mixins/view-module'
import addOrUpdate from './role-menu-add-or-update'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      visible: false,
      isAdd: true,
      menuList: [],
      mixinViewModuleOptions: {
        activatedIsNeed: false,
        getDataListURL: '/system/sysRoleMenu/sqlPage', // 获取角色菜单列表接口
        getDataListIsPage: true,
        deleteURL: '/system/sysRoleMenu', // 删除角色菜单列表项的接口
        deleteIsBatch: true,
        getMenuListURL: 'system/sysMenu/sqlKvs' // 获取菜单列表接口
      },
      loading: null,
      dataForm: {
        username: '',
        roleId: '',
        permissionId: ''
      }
    }
  },
  mounted () {
    this.getMenu()
  },
  methods: {
    init (data) {
      this.dataForm.roleId = data.id
      this.visible = true
      this.getDataList()
    },
    goBack () {
      this.visible = false
      this.$emit('callBack')
    },
    // 获取菜单列表
    getMenu () {
      this.getDataList(this.mixinViewModuleOptions.getMenuListURL, false).then(res => {
        if (res.code !== 0) { return }
        this.menuList = res.data
      })
    },
    // 新增用户
    addUser (data) {
      this.isAdd = true
      this.openDialog('addOrUpdate', data)
    }
  },
  components: {
    addOrUpdate
  }
}
</script>
<style lang="scss" scoped>
.user-dialog {
  background: rgba(0, 0, 0, 0.5);
}
.download {
  text-decoration: none;
  color: #fff;
}
</style>
